/**
 * Copyright (c) OpenSpug Organization. https://github.com/openspug/spug
 * Copyright (c) <spug.dev@gmail.com>
 * Released under the AGPL-3.0 License.
 */
import {observable} from "mobx";
import service from 'libs/request';
import React from "react";
import TableOperation from "../../components/index/TableOperation";

class Store {
  //请求的地址
  @observable uri;
  //弹窗配置
  @observable modalConfig = {};
  //询问框配置
  @observable confirmConfig = {};
  //列表
  @observable records = [];
  //一行
  @observable record = {};
  // 搜索表单配置
  @observable searchConfig = [];
  // 搜索字段map
  @observable searchMap = {};
  // 表格外部操作配置
  @observable actionDivConfig = [];
  //弹窗显示控制
  @observable modalVisible = false;
  //配置文件的请求地址
  @observable configUri = "";
  //获取配置文件的参数
  @observable configParams = {};

  @observable isFetching = false;

  //////////////////////////////////////
  //表格相关
  @observable tableColumns = [];
  //默认的每页条数
  @observable defaultPageSize=undefined;
  //总数
  @observable total=0;
  //页码
  @observable page=1;
  //每页大小
  @observable pageSize=8;
  //表格所有选中的id
  @observable selected_ids = [];


  //显示弹窗
  showModal = (configUri,configParams={},modalConfig={}) => {
      this.modalVisible = true;
      this.configUri = configUri;
      this.configParams = configParams;
      this.modalConfig = modalConfig;
  };

  resetSearchMap = () => {
     this.searchMap = {};
     this.page = 1;
     this.pageSize = 8;
     this.fetchRecords();
  };

  setSearchMap = (map) => {
     this.searchMap = map;
     this.page = 1;
     this.pageSize=8;
  };

  fetchRecords = async () => {
    console.log("请求的首页地址:",this.uri);
    this.isFetching = true;
    this.searchMap["page"] = this.page;
    this.searchMap["pageSize"] = this.pageSize;
    let response = await service.request({
      url:this.uri,
      // url:"/admin/setting/running1/page2/index",
      method:"get",
      params: this.searchMap,
    });

    this.records = response.data.data;
    this.searchConfig = response.data.searchConfig;
    this.actionDivConfig = response.data.actionDivConfig;

    //表格相关设置
    this.tableColumns = response.data.tableConfig.columns;
    this.tableColumns.push({
      title:"操作",
      width:230,
      //渲染表格操作
      render:(info)=>(
          <TableOperation info={info} config={response.data.tableConfig.actions}/>
      )
    });
    this.total = response.data.pagination.totalCount;

    this.isFetching = false

    // return service.request({
    //   url:"/admin/react/test/index",
    //   method:"get",
    //   params: this.searchMap,
    // }).then((response) => {
    //   this.records = response.data.data;
    //   this.searchConfig = response.data.searchConfig;
    //   this.actionDivConfig = response.data.actionDivConfig;
    //   this.tableConfig = response.data.tableConfig;
    //   this.ready = true;
    //   console.log(response.data.tableConfig,'-----------')
    // }).finally(() => this.isFetching = false)
  };

  showForm = (info = {}) => {
    this.formVisible = true;
    this.record = info
  }
}

export default new Store()
